A room marked Persistent is kept when the player leaves it - every object exactly as it was - and is there again on the way back. This page says what is kept, what runs when the player returns, how many rooms the game remembers, and what it costs.
Usually a room is built new every time the player walks into it. Leave a room full of enemies, come back, and they are all there again, because the room was built from the plan you made in the editor. That is the right thing for most rooms.
Sometimes you want the opposite: a shop where you moved the crates, a cave where you beat three of the five bats, a garden where a ball is still rolling. For those, tick Persistent in the Room Editor, in Room Properties (open Transitions and Values). A persistent room is kept when the player leaves it. Every object in it stays exactly as it was: where it stood, how fast it was going, every variable it had, the picture its animation was showing, the time left on its alarms. When the player comes back, those same objects are there, and the room is not built again.
An object that was destroyed while the player was there stays destroyed. A coin the player picked up is still gone.
| Kept, exactly as it was | Not kept - built new every visit, as for any room |
|---|---|
| Every object that was in the room, with its position and speed | The room's tiles. If your code changed a tile with tile_set_index or tile_set_class, the change is gone |
| Every variable of those objects | Pictures made with background_create (see the warning below) |
| Alarms, as they were. They do not count down while the player is away | Tweens that were gliding, and particle effects that were running |
| The picture an animation was showing, and how far along it was | What the room's own creation code set up (see the next section) |
| How far along a path the object was | The room's background color, shroud and size: they come from the room's plan, every time |
Objects that are persistent themselves (the box in the object editor, or the variable persistent) are not part of this. They are never set aside. They come along into the next room, as they always did.
A kept object does not exist while the player is elsewhere. It does not take a turn, it does not draw, it does not bump into anything, and instance_count does not count it. A reference to it reads as gone (instance_exists says no) until the player returns, and then it works again.
Leaving a room and entering one run the same events as always, with two differences for a room that is kept.
| Event | First visit | Coming back to a kept room |
|---|---|---|
| Room End | runs for every object, as the player leaves | the same |
| Cleanup | runs for the objects that are removed | does not run when the room is kept. It runs later, for all of them, if the game forgets the room |
| Create | runs for each object as it is made | does not run. The object was made already |
| An object's own creation code | runs | does not run |
| The room's own creation code | runs | does not run |
| Game Start | only in the game's first room | no |
| Room Start | runs for every object in the room | runs for every object in the room, the kept ones included |
So Room Start is the place for anything that has to happen every time the player arrives: starting this room's music, snapping the camera, hiding a prompt. Anything you put in Create or in creation code happens once, the first time.
Think about anything an object makes that lives outside the room. A picture made with background_create belongs to the game, not to the room, and it is not put away with the room. A kept object's Cleanup does not run when the player leaves, so a picture it made would still be on the screen in the next room. Hide or destroy it in the object's Room End, and show it or make it again in its Room Start.
The game keeps a limited number of rooms: 4, unless you change it in Project Settings, under Engine limits, Rooms the game remembers (from 1 to 16). Leave one more persistent room than that, and the room that was left longest ago is forgotten. Its objects get their Cleanup event and are gone for good, and the next time the player goes there, that room is built new from your plan, as if they had never been.
You can also forget rooms yourself. room_forget(r) forgets one and room_forget_all() forgets them all. save_clear does the same as room_forget_all, so a "new game" does not walk the player back into the last game's rooms.
room_restart builds the room the player is in new, even if it is Persistent. It means "start this room again", and that is what it does.
// Step event of a menu object: "start a new game" forgets every kept room
if (btn_pressed(BTN_START)) {
room_forget_all();
}A kept room lives in the console's working memory, and nowhere else. It is not saved by save_commit: switch the console off and every kept room is forgotten. When the game starts from a save, no room is kept; every room is built from your plan, and your code reads the saved values to put things back, as it always did. If the game loads a different save while it is running, call room_forget_all() first.
A kept room keeps its objects in memory while the player is somewhere else, so the game needs a place for each one. Every object has a number of places, its Max instances. For the room the player is in, that number is the promise it always was. A game with Persistent rooms adds, to each object, the places the kept rooms need: the rooms that place the most of it, as many of them as the game remembers.
You do not have to do this sum. The build does it, and tells you what it did, in a note like obj_bat gets 5 more places than its Max instances (3), so 8 in all. A game that has no Persistent room gets none of this, and is built exactly as it was.
Two things are not counted, because the build cannot know them:
instance_create) and leaves behind in a kept room. If a game makes many, raise that object's Max instances. When an object has no free place, instance_create gives back nothing, as it always has.persistent off for one of them, it can be set aside after all, so give it room.)A kept room keeps what code made in it too, so an object with a small Max instances that your code makes in a Persistent room (a press-A icon, one boss, one shot at a time) keeps its place in that room after the player leaves, and no other room can make one: instance_create gives back nothing and the object never appears. The Problems list warns about this when it can see it. Fix it by destroying the object in its own Room End event (instance_destroy(self);, and make it again in the next room), or by ticking Persistent for that object so it comes along into the next room.
One more thing to know about the extra places. Max instances also stops a second copy of an object being made. Say every room places the player's object, obj_player, and its Max instances is 1: when the player arrives from another room, the room's own copy simply is not made, because there is no free place. With extra places there is one, so an object that is placed in a Persistent room and is meant to exist once can end up twice. Mark that object Persistent in the object editor (it then gets no extra places), or have its Create event check instance_count and destroy itself when it is not the only one.
The more rooms the game remembers, the more memory the objects in them use. The list of kept rooms itself is only a few bytes.